
/*
 * @author Adley, Butcher, and Sanders
 * CSC 285 Final Project
 */

package tetris;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class BoardFrame extends JFrame implements KeyListener {
    private final KeyListener key = this;
    private final BoardPanel board;
    private final TetrisGame game;
  
    
    public BoardFrame(TetrisGame game){
        this.game = game;
        board = new BoardPanel(game);
        add(board);
        setResizable(false);
        Dimension dem = new Dimension(300,500);
        setPreferredSize(dem);
        addKeyListener(key);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

   @Override
    public void keyTyped(KeyEvent ke) {
    }
    
    @Override
    public void keyReleased(KeyEvent ke) {
       game.decreaseDelay();
    }

    @Override
    //get position of all these methods to equal position of shape on moveDown()
    public void keyPressed(KeyEvent event) {
        switch(event.getKeyCode()){
            case KeyEvent.VK_UP:
                
                game.rotate();               
                System.out.println("up");// rotation                
                
                break;
            case KeyEvent.VK_LEFT:
                
                game.moveLeft();   
                System.out.println("left");
                
                break;                
            case KeyEvent.VK_RIGHT:   
                
                game.moveRight();                 
                System.out.println("right");
                
                break;
            case KeyEvent.VK_DOWN:
                
                game.fullDrop();
                System.out.println("down");
                
                break;
        }
    }
    
     public ShapePanel[][] getPanel(){
        return board.getPanel();
    }
     
     public JPanel getPaint(){
        return board.getPaint();
    }
    
}

class BoardPanel extends JPanel{
    private TetrisGame game;
    private final int Rows = 15; //y
    private final int Cols = 10; //x
    private final LayoutManager grid;
    private TileShape currentShape;
    
    // Base color of all the ShapePanels
    private Color baseColor =  Color.LIGHT_GRAY;

    private ShapePanel panel;

    private final ShapePanel[][] panels;
//    private final Color[][] colorArray;
    private final boolean[][] shapeArray;

    public BoardPanel(TetrisGame game){
        //Layout set
        grid = new GridLayout(Rows, Cols);
        //Array of all the ShapePanels
        panels = new ShapePanel[Rows][Cols];
        //Get the ShapeArray from the game
        shapeArray = game.getShapeArray();
        //Get the ColorArray from the game
//        colorArray = game.getColorArray();
        currentShape = game.getCurrentShape();
       
        Dimension dem = new Dimension(300,500);
        setSize(dem);
        
        setLayout(grid);
        
        // Creating the multiple JPanels
        for(int row = 0; row < Rows; row++){
            for(int col = 0; col < Cols; col++){
                ShapePanel sPanel = new ShapePanel();
                sPanel.setCoordinate(row, col);
                sPanel.setBaseColor(baseColor);
                //Sets each Shape Panels color
                sPanel.setVisible(true);
                panels[row][col] = sPanel;
                add(panels[row][col]);
            }
        }

    }

    
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        
        for (int col = 0; col < Cols; col++) {
            for (int row = 0; row < Rows; row++) {    
                if(shapeArray[row][col] == true){
                    panels[row][col].setFill(true);
//                    panels[row][col].setColor(currentShape.getColor());
//                    panels[row][col].setColor(colorArray[row][col]);
                }
            }
        revalidate();
        }  
    }

    public ShapePanel[][] getPanel(){
        return panels;
    }

    public JPanel getPaint(){
        return this;
    }
    
}